//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by 李昊林 on 16/7/26.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *nameArray;
@property (nonatomic, strong) NSMutableArray<NSString *> *selectNameArray;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
@property (nonatomic, strong) UITableView *nameTableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.selectIndexArray = [NSMutableArray array];
    self.selectNameArray = [NSMutableArray array];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:arr];
    self.nameTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _nameTableView.delegate = self;
    _nameTableView.dataSource = self;
    [self.view addSubview:_nameTableView];
    // 注册
    [_nameTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    // 编辑
    // 1. 设置有barButtonItem事编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
}
- (void)editAction{
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
}
#pragma mark - tableView Delegate
// 4.返回cell的编辑状态(样式)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 返回俩个是多选
    return UITableViewCellEditingStyleDelete ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editing){
        // 取出对应下标的名字
        NSString *name = _nameArray[indexPath.row];
        // 添加选中名字到数组
        [_selectNameArray addObject:name];
        // 在全选状态下, 数组中添加对应选中的下标
        [_selectIndexArray addObject:indexPath];
    }
    NSLog(@"走没有de");
    
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editing){
        // 取出对应下标的名字
        NSString *name = _nameArray[indexPath.row];
        // 从选中的数组中移除
        [_selectNameArray removeObject:name];
        // 在全选状态下, 数组中移除对应选中的下标
        [_selectIndexArray removeObject:indexPath];
    }
    NSLog(@"走没有");
}
#pragma mark - tableView DataSource
// Edit
// 2.让tableView进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [_nameTableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        // 要删除选中的数组
        // 删除数据源
        [_nameArray removeObjectsInArray:_selectNameArray];
        // 删除cell
        [_nameTableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationTop];
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
        NSLog(@"完成");
        
    }
}
// 3.控制tableView时候能够编辑, 默认值是YES
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
// 更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
// 滑动删除只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        //#waring 如果对tableView进行添加/删除,先修改数据源,再去进行对应的添加/删除
        // 1. 修改数据源
        [_nameArray removeObjectAtIndex:indexPath.row];
        // 2. 对应的删除cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // 修改数据源
        [_nameArray insertObject:@"王煌" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        NSLog(@"插入");
    }
    // tableView 不论进行删除添加
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    cell.textLabel.text = self.nameArray[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
